package com.yxzr.youyou.retrofit;

import org.reactivestreams.Publisher;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 18354 on 2018-04-08.
 */

public class RequestUtil {
    private static RequestUtil mRetrofitFactory;
    private static  RetrofitService mAPIFunction;

    private RequestUtil(){

        OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(20000, TimeUnit.SECONDS)//设置读取超时时间
                .writeTimeout(20000, TimeUnit.SECONDS)//设置写入超时时间
                .addInterceptor(InterceptorUtil.HeaderInterceptor())//添加其他拦截器
                .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(DataUrl.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(mOkHttpClient) // 打印请求参数
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
                .build();

        mAPIFunction = retrofit.create(RetrofitService.class);
    }

    public static RequestUtil getInstence(){
        if (mRetrofitFactory==null){
            synchronized (RequestUtil.class) {
                if (mRetrofitFactory == null)
                    mRetrofitFactory = new RequestUtil();
            }
        }
        return mRetrofitFactory;
    }
    public RetrofitService API(){
        return mAPIFunction;
    }


    private static FlowableTransformer<ResponseBody, ResponseBody> ioMainDownload(){
        return new FlowableTransformer<ResponseBody, ResponseBody>() {
            @Override
            public Publisher<ResponseBody> apply(Flowable<ResponseBody> upstream) {
                return upstream.subscribeOn(Schedulers.io()).
                        observeOn(Schedulers.io()).
                        observeOn(Schedulers.computation()).
                        map(new Function<ResponseBody, ResponseBody>() {
                            @Override
                            public ResponseBody apply(ResponseBody responseBody) throws Exception {
                                return responseBody;
                            }
                        }).
                        observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static FileDownLoadSubscriber downLoad(FileDownLoadSubscriber subscriber, String url){
        return mAPIFunction.downLoadApk(url).
                compose(ioMainDownload()).
                subscribeWith(subscriber);
    }

}
